import { GuideCfg, ICondition, IGuide } from "../../config/GuideCfg"
import { PrefabPath } from "../../config/PrefabPath";
import { GamingStore } from "../store/GamingStore";
import { UserStore } from "../store/UserStore";
import UIManager from "./UIManager";

export default class GuideManager {
    /**
     * TODO:
     * 1.点击引导 force = true
     *    强制引导  按钮位置 点击区域
     *    非强制引导 全屏点击到下一个引导
     * 2.显示旁白 是否富文本
     *    显示内容 guideName   Text or RichText nodeName  Pos  
     *    定时器 时间到才可以继续点击 timeSpan = 0
     * 3.触发条件
     * level <= 1;
     * 4.处理存储数据
     */

    /**
     * 每次进场景 或者弹窗 检测 当前是否有引导未完成
     * 根据条件检测
     * 
     * 
     * 
     */
    /**
     * 当前引导数据
     */
    private static _ins: GuideManager;

    public static get Ins() {
        return this._ins = this._ins ? this._ins : new GuideManager();
    }


    guideIng: boolean = false;
    curConfig: IGuide = null;
    preFingerPos: cc.Vec3 = null;
    /**
     * 引导触发器
     */
    guideTrigger() {
        let config = this.getGuideInfoByCurScene()
        if (!config) return;
        if (config.group) {
            let guides = this.getGuideInfoByGroup(config.group)
            if (guides.length) {
                config = guides[0];
                let pass = this.checkAllCondition(config.condition);
                if (pass && !this.guideIng) {
                    this.guideIng = true;
                    UIManager.show(PrefabPath.guideLayer, config);
                }
            }
        } else {
            let pass = this.checkAllCondition(config.condition);
            if (pass && !this.guideIng) {
                this.guideIng = true;
                UIManager.show(PrefabPath.guideLayer, config);
            }
        }
    }
    /**检测所有的条件是否都满足 */
    private checkAllCondition(conditions: ICondition[]) {
        let allPass = true;
        for (let i = 0; i < conditions.length; i++) {
            const condition = conditions[i];
            let pass = this.checkCondition(condition);
            if (!pass) {
                allPass = false;
                break;
            }
        }
        return allPass
    }
    /**
     * 检测是否满足条件
     */
    private checkCondition(condtionInfo: ICondition) {
        
        let passCheck: boolean = false;
        switch (condtionInfo.action) {
            case 'level':
                passCheck = this.checkLevel(condtionInfo)
                break;
            case 'level_success':
                passCheck = this.checkFirstPassLevel(condtionInfo)
                break;
            default:
                break;
        }
        return passCheck;
    }
    /**
     * 检测关卡
     * @param condtionInfo 
     */
    private checkLevel(condtionInfo: ICondition) {
        let curCount = UserStore.Ins.level;
        if (this._judge(condtionInfo.judge, curCount, condtionInfo.count)) {
            return true;
        }
        return false;
    }
    /**
     * 检测第一次通关
     * @param condtionInfo 
     */
    private checkFirstPassLevel(condtionInfo: ICondition) {
        let curCount = +GamingStore.Ins.isPass;
        if (this._judge(condtionInfo.judge, curCount, condtionInfo.count)) {
            return true;
        }
        return false;
    }
    /**
     * 判断条件是否满足
     * @param judge > or < or =
     * @param curNum 当前到数值
     * @param needNum 目标数值
     */
    private _judge(judge: string, curNum: number, needNum: number) {
        if (judge == ">") {
            if (curNum > needNum) {
                return true;
            }
        } else if (judge == "<") {
            if (curNum < needNum) {
                return true;
            }
        } else if (judge == "=") {
            if (curNum == needNum) {
                return true;
            }
        }
        return false;
    }

    get nextGuideInfo(): IGuide {
        let nextInfo: IGuide = null;
        if (this.curConfig.next) {
            GuideCfg.forEach(guideInfo => {
                if (guideInfo.guideId == this.curConfig.next) {
                    nextInfo = guideInfo
                }
            });
        }
        return nextInfo
    }

    /**
     * 获取当前打开场景是否有可执行的引导
     */
    getGuideInfoByCurScene(): IGuide {
        let guide: IGuide = null;
        let guides = GuideCfg.filter(guideInfo => {
            return guideInfo.scene == UIManager.current.path
        });
        if (guides.length) {
            for (let i = 0; i < guides.length; i++) {
                const item = guides[i];
                if (UserStore.Ins.guideId < item.guideId) {
                    guide = item
                    break;
                }
            }
        }
        return guide;
    }

    /**
    * 获取当前打开场景是否有可执行的引导
    */
    getGuideInfoByGroup(groupId: number): IGuide[] {
        let guides = [];
        for (let i = 0; i < GuideCfg.length; i++) {
            const item = GuideCfg[i];
            if (item.group == groupId) {
                guides.push(item)
            }
        }
        return guides;
    }
}